package com.tongji.seller.view
{
	import com.tongji.seller.events.AddStoreSceneEvent;
	import com.tongji.seller.events.GetStoreSavedSceneEvent;
	import com.tongji.seller.events.GetStoreSystemSceneHotStateEvent;
	import com.tongji.seller.events.GetStoreSystemSceneStateEvent;
	import com.tongji.seller.events.GetTypesEvent;
	import com.tongji.seller.events.SearchIdEvent;
	import com.tongji.seller.events.utilsEvent.ChangeStepEvent;
	import com.tongji.seller.events.utilsEvent.PageChangeEvent;
	import com.tongji.seller.model.GetSystemSceneModel;
	import com.tongji.seller.model.SearchSceneModel;
	import com.tongji.seller.model.StoreSceneModel;
	import com.tongji.seller.model.TypeModel;
	import com.tongji.seller.model.UserModel;
	import com.tongji.seller.utils.PageTools;
	import com.tongji.seller.utils.ShowCustomToolTip;
	import com.tongji.seller.view.comp.PageComp;
	import com.tongji.seller.vo.Type;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.charts.renderers.DiamondItemRenderer;
	import mx.controls.Alert;
	import mx.events.CollectionEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author jiarong
	 * 2011-11-26
	 *
	 */
	
	public class FirstStepViewMediator extends Mediator
	{
		[Inject]
		public var view:FirstStepView;
		
		[Inject]
		public var getSystemSceneModel:GetSystemSceneModel;
		
		[Inject]
		public var typeModel:TypeModel;
		
		[Inject]
		public var searchSceneModel:SearchSceneModel;
		
		[Inject]
		public var userModel:UserModel;
		
		[Inject]
		public var storeSceneModel:StoreSceneModel;
		private var tempNumber:Number = 0;
		private var leftIndex:Number = 0;
		private var rightIndex:Number = 3;
		private var pageIndex:Number = 1;
		
		public var cdIdArr:Array = new Array();
		public var fgIdArr:Array = new Array();

		override public function onRegister():void
		{		
			view.placeList.createDateProvider(typeModel.placeArray);
			view.styleList.createDateProvider(typeModel.styleArray);
			
			addContextListener(GetTypesEvent.UPADATE_PLACES, bindingHandler);
			addContextListener(GetTypesEvent.UPDATE_STYLES,bindingHandler);
			addViewListener(SearchIdEvent.SELECT_ALL_PLACEBOX, selectAllPlaceBoxHandler);
			addViewListener(SearchIdEvent.SELECT_ALL_STYLEBOX, selectAllStyleBoxHandler);
			
			view.searchBtn.addEventListener(MouseEvent.CLICK, searchSceneHandler);
			view.placeList.addEventListener(SearchIdEvent.SEARCH_CDID, searchSceneIdHandler);
			view.styleList.addEventListener(SearchIdEvent.SEARCH_FGID, searchStyleIdHandler);
			view.placeList.addEventListener(SearchIdEvent.DELETE_CDID, deletecdIdHandler);
			view.styleList.addEventListener(SearchIdEvent.DELETE_FGID, deletefgIdHandler);
			//this.addContextListener(ShowComplementInforEvent.SHOW_COMPLEMENT_INFOR, showToolTip, ShowComplementInforEvent);
			
			eventMap.mapListener(view.toSecondBtn, MouseEvent.CLICK, toSecondStepHandler, MouseEvent);
	       // BindingUtils.bindProperty(view.sceneView.sceneViewList, "dataProvider", getSystemSceneModel, "getSceneArr");

		    BindingUtils.bindProperty(view.sceneView.sceneViewList, "dataProvider", storeSceneModel, "systemSceneArray");
			addContextListener(PageChangeEvent.UPDATE_PAGE, test, PageChangeEvent);
			//addContextListener(GetStoreSavedSceneEvent.UPDATE_SAVED_SCENE, createDataHandler, GetStoreSavedSceneEvent);

			view.sceneView.scenePageComp.createDataProvider([storeSceneModel.pageTools.nums, storeSceneModel.pageTools.pageArray, storeSceneModel.pageTools.pageCount, pageIndex]);
	}
		
		protected function test(event:PageChangeEvent):void
		{
			view.sceneView.scenePageComp.createDataProvider([storeSceneModel.pageTools.nums, storeSceneModel.pageTools.pageArray, storeSceneModel.pageTools.pageCount, event.pageIndex]);
		}
		
		/**
		 * @author 跳转至第二步 
		 * @param event
		 * 
		 */		
		private function toSecondStepHandler(event:MouseEvent):void
		{	
			if (storeSceneModel.selectedSceneArray.length == 0)
			{
				Alert.show("请您先选择场景");
			}
			else
			{
				if (storeSceneModel.state == 1)
				{
					dispatch(new ChangeStepEvent(ChangeStepEvent.TO_THIRD_STEP));
				}
				else
				{
					/*var itemCount:int = getSystemSceneModel.selectSceneArray.length;
					var sIdArray:Array = new Array();
					for each(var scene:Object in storeSceneModel.savedSceneArray)
					{
						sIdArray.push(scene.sid);
					}
					dispatch(new AddStoreSceneEvent(AddStoreSceneEvent.ADD_STORE_USER_SYSTEM_SCENE, userModel.currentUserID, 0, sIdArray));*/
					dispatch(new ChangeStepEvent(ChangeStepEvent.TO_SECOND_STEP));
				}
			}
			
		}
		
		private function bindingHandler(event:GetTypesEvent):void
		{
			view.placeList.createDateProvider(typeModel.placeArray);
			view.styleList.createDateProvider(typeModel.styleArray);
			var page:PageTools = new PageTools(getSystemSceneModel.recordCount, 10, 18, pageIndex);
			view.sceneView.scenePageComp.createDataProvider([page.nums, page.pageArray, page.pageCount, pageIndex]);		
		}
		
		/**
		 *获取与场地中所选checkBox对应的对象的场景Id 
		 * @param event
		 * 
		 */				
		private function searchSceneIdHandler(event:SearchIdEvent):void
		{
			cdIdArr.push(event.scdId);
			for (var i:int = 0; i < typeModel.placeArray.length; i++)
			{
				if (typeModel.placeArray.getItemAt(i).typeId == event.scdId)
				{
					typeModel.placeArray.getItemAt(i).selected = true;
					break;
				}
			}
		}
		
		/**
		 * 获取与风格中所选checkBox对应的对象的风格Id
		 * @param event
		 * 
		 */		
		
		private function searchStyleIdHandler(event:SearchIdEvent):void
		{
			fgIdArr.push(event.sfgId);
			for (var i:int = 0; i < typeModel.styleArray.length; i++)
			{
				if (typeModel.styleArray.getItemAt(i).typeId == event.scdId)
				{
					typeModel.styleArray.getItemAt(i).selected = true;
					break;
				}
			}
		}
		
		/**
		 *删除与场地中所选checkBox对应的对象的场景Id 
		 * @param event
		 * 
		 */	
		
		private function deletecdIdHandler(event:SearchIdEvent):void
		{ 
			for (var i:int = 0; i < cdIdArr.length; i++)
			{
				if (event.scdId == cdIdArr[i])
				{
					cdIdArr.splice(i, 1);
				}
			}
			for (var j:int = 0; j < typeModel.placeArray.length; j++)
			{
				if (typeModel.placeArray.getItemAt(j).typeId == event.scdId)
				{
					typeModel.placeArray.getItemAt(j).selected = false;
					break;
				}
			}
		}
		
		/**
		 * 删除与风格中所选checkBox对应的对象的风格Id
		 * @param event
		 * 
		 */		
		
		private function deletefgIdHandler(event:SearchIdEvent):void
		{
			for (var i:int = 0; i < fgIdArr.length; i++)
			{
				if (event.sfgId == fgIdArr[i])
				{
					fgIdArr.splice(i, 1);
				}
			}
			for (var j:int = 0; j < typeModel.styleArray.length; j++)
			{
				if (typeModel.styleArray.getItemAt(j).typeId == event.scdId)
				{
					typeModel.styleArray.getItemAt(j).selected = false;
					break;
				}
			}
		}
		
		/**
		 *根据场景和风格搜索所需条件的场景图片并显示 
		 * @param event
		 * 
		 */	
		
		private function searchSceneHandler(event:MouseEvent):void
		{
			storeSceneModel.stateVar = 1;
			
			searchSceneModel.cdIdStr = cdIdArr[0];
			searchSceneModel.fgIdStr = fgIdArr[0];
			for each(var type:Type in view.placeList.typeArray)
			{
				if(type.selected)
				{
					for(var m:int = 1; m < cdIdArr.length; m++)
					{
						if (m == 0)
						{
							searchSceneModel.cdIdStr = type.typeId as String;
						}
						else
						{
							searchSceneModel.cdIdStr = searchSceneModel.cdIdStr +"," + type.typeId as String;
						}
					}
				}
			}
			
			
			for(var i:int = 1; i < cdIdArr.length; i++)
			{
				if (i == 0)
				{
					searchSceneModel.cdIdStr = cdIdArr[i];
				}
				else
				{
					searchSceneModel.cdIdStr = searchSceneModel.cdIdStr +"," + cdIdArr[i] as String;
				}
			}
			
			for(var j:int = 1; j < fgIdArr.length; j++)
			{
				if (j == 0)
				{
					searchSceneModel.fgIdStr = fgIdArr[j];
				}
				else
				{
					searchSceneModel.fgIdStr = searchSceneModel.fgIdStr + "," + fgIdArr[j] as String;
				}
			}
			//dispatch(new SearchSceneEvent(SearchSceneEvent.SEARCH_ALL_SCENES, searchSceneModel.cdIdStr, searchSceneModel.fgIdStr, 1,18, null));
			dispatch( new GetStoreSystemSceneStateEvent(GetStoreSystemSceneStateEvent.GET_STORE_SYSTEM_SCENE_STATE, userModel.currentUserID, searchSceneModel.cdIdStr, searchSceneModel.fgIdStr));
			storeSceneModel.searchScenePageIndex = 1;
			view.sceneView.allSceneBtn.selected = true;
			//BindingUtils.bindProperty(view.sceneView.sceneViewList, "dataProvider", searchSceneModel, "SearchSceneArr");
		}
		
		private function selectAllPlaceBoxHandler(event:SearchIdEvent):void
		{
			if (event.selected)
			{
				for (var i:int = 0; i < typeModel.placeArray.length; i++)
				{
					typeModel.placeArray[i].selected = true;
				}
				view.placeList.createDateProvider(typeModel.placeArray);
			}
			else
			{
				for (var j:int = 0; j < typeModel.placeArray.length; j++)
				{
					typeModel.placeArray[j].selected = false;
				}
				view.placeList.createDateProvider(typeModel.placeArray);
			}
		}
		
		private function selectAllStyleBoxHandler(event:SearchIdEvent):void
		{
			if (event.selected)
			{
				for (var m:int = 0; m < typeModel.styleArray.length; m++)
				{
					typeModel.styleArray[m].selected = true;
				}
				view.styleList.createDateProvider(typeModel.styleArray);
			}
			else
			{
				for (var n:int = 0; n < typeModel.styleArray.length; n++)
				{
					typeModel.styleArray[n].selected = false;
				}
				view.styleList.createDateProvider(typeModel.styleArray);
			}
		}
		
	}
}